﻿using DataAccess.DAL;
using DataAccess.DTO;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    public class LoginController : Controller
    {
        private AgencyUsersDAL userDAL = new AgencyUsersDAL();

        private PromoterDAL proDal = new PromoterDAL();
        
        public ActionResult Index()
        {
            return View();  
        }

        public ActionResult Authorize()
        {
            return View();
        }

        public ActionResult loginout()
        {
            CookieHelp.DeleteCookie(CookieHelp.UserCookieKey);
            return Redirect("/login.html");
        }
        /// <summary>
        /// 推广者登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Logined(string UserName, string Password)
        {
            if (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password))
            {
                UserSession session = proDal.LoginSession(UserName.Trim(), Password);
                if (session == null)
                {

                    return Json(new Result { State = false, Message = "用户名或密码不正确" });
                }
                CookieHelp.DeleteCookie(CookieHelp.UserCookieKey);
                string cookie = XYD.Common.EncodeSecurity.EncryptString(XYD.Common.JsonHelper.SerializerJson<UserSession>(session));
                CookieHelp.WriteCookie(CookieHelp.UserCookieKey, cookie, 7);
                return Json(new Result { State = true, Message = "登录成功" });
            }
            return Json(new Result { State = false, Message = "登录失败" });
            
        }
	}
}